package com.gk.util
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	/**
	 *加载数据 URLLoader （访问后台，加载xml 。。） 
	 * @author Nestor
	 * 
	 */	
	public class LoadData
	{
		private var okFun:Function;
		private var errorFun:Function;
		private var loader:URLLoader;
		/**
		 * 
		 * @param okFun (参数 dobj)
		 * @param errFun (参数  IOErrorEvent)
		 * 
		 */		
		public function LoadData(url:String,okFun:Function,param:Object = null,errFun:Function =null)
		{
			
			this.okFun = okFun;
			this.errorFun = errFun;
			
			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE,comHadnler);
			loader.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			
			var rqs:URLRequest = new URLRequest(url);
			if(param !=null){
				rqs.data = param;
			}
			loader.load(rqs);
		}
		
		private function errorHandler(e:IOErrorEvent):void
		{
			if(this.errorFun){
				this.errorFun(e);
				this.errorFun = null;
			}
			clear();
		}
		
		private function comHadnler(e:Event):void
		{
			var obj:Object = loader.data;
			if(this.okFun){
				this.okFun(obj);
				this.okFun = null;
			}
			
			clear();
		}
		
		private function clear():void
		{
			// TODO Auto Generated method stub
			loader.removeEventListener(Event.COMPLETE,comHadnler);
			loader.removeEventListener(IOErrorEvent.IO_ERROR,errorHandler);
		}		
		
	}
}